/*
 *	OpenSource code or fragments by agile webentwicklung, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'avocaod systems, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 */
package de.fuhrmeister.browserchooser.view;

import java.awt.Component;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.util.EventListener;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * Browser view class used in the settings dialog.
 * 
 *	@date 20.11.2008
 *	@author	Marcus Fuhrmeister
 */
public class BrowserView extends JScrollPane implements View {

	private static final long serialVersionUID = 397937347959345685L;
	private final JPanel mainPanel;
	private JLabel nameLabel;
	private JTextField nameTextField;
	private JLabel iconLabel;
	private JLabel iconImage;
	private JButton iconButton;
	private JButton newButton;
	private JPanel versionPanel;
	private int browserID;
	private JButton deleteButton;
	private String iconImagePath;

	/**
	 * The constructor to create a new browser view.
	 * Calls to init the components.
	 * 
	 * @param width the width of the the view
	 */
	public BrowserView(final int width) {
		super();
		this.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		this.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		mainPanel = new JPanel();
		final Border b = Borders.DIALOG_BORDER;
		final Insets i = b.getBorderInsets(this);
		final int colWidth = (width - (i.left + i.right + View.SCROLLBARWIDTH)) / 4;
		final FormLayout layout = new FormLayout(colWidth + "px, " + colWidth + "px, " + colWidth + "px, " + colWidth + "px", "pref, 12px, pref, 24px, pref, 12px, pref");
		mainPanel.setLayout(layout);
		mainPanel.setBorder(b);
		final CellConstraints cc = new CellConstraints();
		this.initComponents(cc);
		this.setViewportView(mainPanel);

	}

	/**
	 * Inits the view components.
	 * 
	 * @param cc the CellConstraints
	 */
	private void initComponents(final CellConstraints cc) {
		// browser name
		nameLabel = new JLabel();
		mainPanel.add(nameLabel, cc.xy(1, 1));
		nameTextField = new JTextField();
		mainPanel.add(nameTextField, cc.xyw(2, 1, 2));
		// browser icon
		iconLabel = new JLabel();
		mainPanel.add(iconLabel, cc.xy(1, 3));
		iconImage = new JLabel();
		mainPanel.add(iconImage, cc.xy(2, 3));
		iconButton = new JButton();
		mainPanel.add(iconButton, cc.xy(4, 3));
		newButton = new JButton();
		mainPanel.add(newButton, cc.xyw(1, 5, 2));
		deleteButton = new JButton();
		deleteButton.setVisible(false);
		mainPanel.add(deleteButton, cc.xy(4, 5));
		versionPanel = new JPanel();
		final BoxLayout bl = new BoxLayout(versionPanel, BoxLayout.Y_AXIS);
		versionPanel.setLayout(bl);
		mainPanel.add(versionPanel, cc.xyw(1, 7, 4));
	}

	/**
	 * Sets the browser name label and text field value.
	 * 
	 * @param label the browser name label
	 * @param text the browser name text field
	 */
	public void setBrowserNameProperties(final String label, final String text) {
		nameLabel.setText(label);
		nameTextField.setText(text);
	}

	/**
	 * Sets the browser icon label value, icon path, image icon and icon button value.
	 * 
	 * @param label the browser icon label
	 * @param iconPath the browser icon path
	 * @param icon the browser image icon
	 * @param button the browser icon button
	 */
	public void setBrowserIconProperties(final String label, String iconPath, final Icon icon, final String button) {
		iconLabel.setText(label);
		iconImagePath = iconPath;
		iconImage.setIcon(icon);
		iconButton.setActionCommand(button);
		iconButton.setText(button);
	}

	/**
	 * Sets the browser button value to create a new browser version
	 * 
	 * @param button the button to create a new browser version
	 */
	public void setBrowserNewProperties(final String button) {
		newButton.setText(button);
		newButton.setActionCommand(button);
	}
	/**
	 * Sets the browser button value to delete the browser
	 * 
	 * @param button the button to delete the browser
	 */
	public void setBrowserDelete(final String button) {
		deleteButton.setText(button);
		deleteButton.setVisible(true);
		deleteButton.setActionCommand(button);
	}

	/**
	 * Returns the browser name text field value.
	 * 
	 * @return the browser name text field value
	 */
	public String getBrowserName() {
		return nameTextField.getText();
	}

	/**
	 * Returns the browser icon image path.
	 * 
	 * @return the browser icon image path
	 */
	public String getBrowserIcon() {
		return iconImagePath;
	}

	/**
	 * Sets the browser id.
	 * 
	 * @param i the browser id
	 */
	public void setBrowserID(final int i) {
		browserID = i;
	}

	/**
	 * Returns the browser id. 
	 * 
	 * @return the browser id
	 */
	public int getBrowserID() {
		return browserID;
	}

	/**
	 * Adds a new <code>BrowserVersionView</code> to the browser version panel.
	 * 
	 * @param versionView the new version view
	 */
	public void addVersionView(final BrowserVersionView versionView) {
		versionPanel.add(versionView);
	}

	/**
	 * Registers the listeners.
	 */
	public void registerListener(final EventListener el) {
		if (el instanceof MouseListener) {
			nameTextField.addMouseListener((MouseListener) el);
		}
		if (el instanceof FocusListener) {
			nameTextField.addFocusListener((FocusListener) el);
		}
		if (el instanceof ActionListener) {
			iconButton.addActionListener((ActionListener) el);
			newButton.addActionListener((ActionListener) el);
			deleteButton.addActionListener((ActionListener) el);
		}
	}

	/**
	 * Returns the components in the version panel.
	 * 
	 * @return the components in the version panel
	 */
	public Component[] getBrowserVersions() {
		return versionPanel.getComponents();
	}
}
